﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using IO = System.IO;
using System.Net;

namespace CrazyApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private const string MYSTRING = "Hello world!";

        public Thread CrazyThread { get; set; }

        private bool running;
        private double value;
        private int sleepInterval;
        private string tempFile;

        public double Value
        {
            get { return this.value; }
            set
            {
                this.value = value;
                NotifyPropertyChanged("Value");
            }
        }

        public int SleepInterval
        {
            get { return this.sleepInterval; }
            set
            {
                this.sleepInterval = value;
                NotifyPropertyChanged("SleepInterval");
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.Closing += MainWindow_Closing;
            this.DataContext = this;
            this.SleepInterval = 10;
            this.tempFile = IO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), IO.Path.GetRandomFileName());
            IO.File.WriteAllText(tempFile, MYSTRING);
        }

        private void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            this.running = false;
        }

        private void CrazyMethod()
        {
            while (running)
            {
                for (int i = 1; i < 100 && running; i++)
                {
                    for (int j = 1; j < 1000 && running; j++)
                    {
                        this.Value = Math.Pow(j, i);
                        Thread.Sleep(Convert.ToInt32(SleepInterval));
                    }
                }
            }
        }

        private void ButtonRead_Click(object sender, RoutedEventArgs e)
        {
            IO.File.ReadAllBytes(tempFile);
        }

        private void ButtonWrite_Click(object sender, RoutedEventArgs e)
        {
            IO.File.WriteAllText(tempFile, MYSTRING);
        }

        private void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            this.running = !this.running;

            (sender as Button).Content = this.running ? "Stop" : "Start";

            if (this.running)
            {
                this.CrazyThread = new Thread(new ThreadStart(CrazyMethod));
                this.CrazyThread.Start();
            }
        }

        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void ButtonSendData_Click(object sender, RoutedEventArgs e)
        {
            new Thread(new ThreadStart(delegate()
            {
                new WebClient().DownloadString("http://www.google.com/");
            })).Start();
        }

        private void ButtonReceiveData_Click(object sender, RoutedEventArgs e)
        {
            new Thread(new ThreadStart(delegate()
            {
                try
                {
                    new WebClient().UploadString("http://www.google.com/", MYSTRING);
                }
                catch { }
            })).Start();
        }
    }
}
